layui.use('element', function () {
    var element = layui.element;
});
//
layui.config({
    base:'/asserts/mods/'
    ,version: '1.0'
});
layui.use(['layer', 'form', 'layarea','upload','util'], function () {

    var layer = layui.layer
        , form = layui.form
        , layarea = layui.layarea
        , $ = layui.jquery
        ,util = layui.util
        ,upload = layui.upload;

        var flagPhone=false;
        var flagCode=false;
        var phonenumber = $("#phonenumber");
        var identityCode = $("#identityCode");
        $("#p_sendMes").hide();
        /*验证手机号*/
        //,var name;;

    layarea.render({
        elem: '#area-picker',
        data:{
            province: province,
            city: city,
            county: county
        },
        change: function (res) {
            //选择结果

        }
    });
    form.verify({
        //判断原密码是否正确
        pwdisright: function(value)
        {
            if ($("#realpwd").val() !== value) {
                return '原密码错误！';
            }
        },
        //判断原密码与新密码是否一致
        newpwd: function (value) {
            if ($("#pwd").val() === value) {
                return '密码不能与原密码一致';
            }
        }
        //判断新密码与再次输入的密码是否一致
        ,repwd: function (value) {
            //var newpwd = $("#newpwd").val();
            if ($("#newpwd").val() !== value) {
                return '两次输入的密码不一致';
            }
        }
        ,phoneExist: function (value) {  //手机号是否存在

            if(isExist(value))
            {
                return '该手机号已存在！请重新输入';
            }
        }
        ,codeIsRight:function (value) {     //判断验证码位数
            if (value.length !==4){

                return '验证码为四位!';
            }
        }


    });
    //判断手机号是否存在
    function isExist(phonenumber) {
        var is = false;
        $.ajax({
            type:"get",
            async:false,
            url:"/judgeByPhone",
            data:{
                "phone":phonenumber
            },
            success:function (result) {
                if(result===1){

                    is = true;
                }else {
                    is = false;
                }
            },
            error:function (e) {

            }
        });

        return is;

    }

    var uploadInst = upload.render({
        elem: '#upload-img',    //绑定上传图片按钮
        accept: 'images',        //只允许上传图片类型
        acceptMime:'image/jpg,image/jpeg,image/png',    //规定打开文件选择框时筛选的文件类型
        multiple: false,        //不允许多文件上传
        exts:'jpg|jpeg|png',    //允许上传这几种格式的文件
        auto: true,             //自动上传
        size: 2048,
        url:'/uploadImage',  //上传接口
        choose:function (obj) {
            var files = obj.pushFile();
            obj.preview(function (file,result) {

                var imgobj = new Image(); //创建一个img对象
                imgobj.src=result;
                imgobj.className='thumb';
                imgobj.onclick = function (result) {
                    //单击预览
                    img_prev.src=this.src;
                    var w =$(window).width()-42,h=$(window).height()-42;
                    layer.open({
                        title:'预览',
                        type: 1,
                        area: [w,h],//宽高
                        content: $('#prevModal')
                    });
                };
                document.getElementById("img_prev").appendChild(imgobj);
            });
        },
        done: function (res) {

            if(res.code  === 0)
            {
                layer.msg("上传成功！");
                img_prev.src=res["link"];
                $("userImg").attr('src',res["link"]);
                window.location.href='/user/edit.html'
            }
        }
        ,error: function () {
            return '上传失败'
        }
    });

    let sendMes=$("#sendMes");
    sendMes.on('click',function () {
        let phone=phonenumber.val();
        if(phone === "")
        {
            layer.msg('手机号为空');
            return false;
        }
        else if (isExist(phone)){
            layer.msg('该手机号已存在');
            return false;
        }
        else if (phone.length !== 11){
            layer.msg('请输入11位手机号');
            return false;
        }
        else
        {
            //console.log(phone);
            flagPhone = true;
            layer.msg('验证码已发送至您的手机！',{icon:6});
            sendMes.addClass('layui-btn-disabled').attr("disabled");
            //验证倒计时

            let send_second = $("#send_second");
            send_second.text(60);
            layui.$("#p_sendMes").show();
            var setTime;
            var time=parseInt(send_second.text());
            setTime=setInterval(function () {
                if (time <= 0){
                    //$("#sendMes").text("重新发送");
                    $("#send_second").text(0);
                    $("#p_sendMes").hide();
                    clearInterval(setTime);
                    sendMes.removeClass("layui-btn-disabled").attr("disabled");
                    return;
                }
                time--;
                send_second.text(time);
            },1000);


            // /send
            $.ajax({
                type: "get",
                url: "/send",
                //async:false,
                data: {"phone": phone},
                success: function (result) {
                    if (result === 0) {
                        flagCode = false;
                    }
                    flagCode = true;
                },
                error: function (e) {

                }
            });
        }
    });

    $("#changePhone").on('submit',function (data) {
        if(flagPhone ===false ||flagCode === false)
        {
            layer.msg("请获取验证码");
            return false;
        }

        ///changePhoneNumber
        if (flagPhone && flagCode){
                $.ajax({
                    type: "post",
                    url: "/changePhoneNumber",
                    data: {
                        "phone":phonenumber.val(),
                        "code":identityCode.val()
                    },
                    success:function (result) {
                        if(result===0){
                            layer.msg("验证码不正确");
                            //console.log("验证码不正确");
                            flagCode = false;
                            return false;
                        }else {
                            layer.msg("修改成功");
                            window.location.href = '/main/index.html';
                        }

                    },
                    error:function (e) {

                    }

                });
            }
        return false;
    });
});


